// 给定一个二叉树，返回所有从根节点到叶子节点的路径。

// 说明: 叶子节点是指没有子节点的节点。

// 示例:

// 输入:

//    1
//  /   \
// 2     3
//  \
//   5

// 输出: ["1->2->5", "1->3"]

// 解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->3

#include "stdc++.h"

// Definition for a binary tree node.
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

/* 深度优先搜索
*/
class Solution {
public:
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> res{};
        binaryTreePaths(root, "", res);
        return res;
    }
    void binaryTreePaths(TreeNode* root, string s, vector<string>& res) {
        if (root) {
            s += to_string(root->val);
            if (root->left == nullptr && root->right == nullptr) {
                res.push_back(s);
            } else {
                s += "->";
                binaryTreePaths(root->left, s, res);
                binaryTreePaths(root->right, s, res);
            }
        }
    }
};

/* 广度优先搜索
*/
class Solution {
public:
    vector<string> binaryTreePaths(TreeNode* root) {
        if (root == nullptr) {
            return {};
        }
        vector<string> paths{};
        queue<TreeNode*> node_queue{};
        queue<string> path_queue{};
        node_queue.push(root);
        path_queue.push(to_string(root->val));
        while (!node_queue.empty()) {
            auto node = node_queue.front();
            auto path = path_queue.front();
            node_queue.pop();
            path_queue.pop();
            if (node->left == nullptr && node->right == nullptr) {
                paths.push_back(path);
            } else {
                if (node->left) {
                    node_queue.push(node->left);
                    path_queue.push(path + "->" + to_string(node->left->val));
                }
                if (node->right) {
                    node_queue.push(node->right);
                    path_queue.push(path + "->" + to_string(node->right->val));
                }
            }
        }
        return paths;
    }
};